package timein8;

import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import org.testng.annotations.Test;

public class Clock1 {

    public static void main(String[] args) {
        clock();
        System.out.println();
        zoneClock();
        System.out.println();

        transform();
        System.out.println();

        calPeriod();
        System.out.println();

        calDay();
        System.out.println();
    }

    public static void clock() {
        Clock clock = Clock.systemUTC();
        Clock clock2 = Clock.systemDefaultZone();
        System.out.println("clock.instant" + clock.instant());


        System.out.println("由systemUTC赋值的Clock得到的毫秒数和currentTimeMillis得到的毫秒数");
        System.out.println("systemDefaultZone " + clock2.millis());
        System.out.println("currentTimeMillis " + System.currentTimeMillis());
        System.out.println("systemUTC         " + clock.millis());

    }



    public static void zoneClock() {
        Clock clock = Clock.systemDefaultZone();
        System.out.println(clock.instant());

//        ZoneId zoneId =

        Clock clock2 = Clock.systemUTC().withZone(ZoneId.of("UTC+9"));
        System.out.println(clock2.instant());

        ZonedDateTime dateTime = ZonedDateTime.now();
        System.out.println(dateTime);

    }

    private void t2() {
        Instant instant = Instant.ofEpochMilli(System.currentTimeMillis());
        LocalDateTime.ofInstant(instant, ZoneId.systemDefault());

    }

    /**
     * 毫秒 转换 为日期和时间
     */
    public static void transform() {
        System.out.println(" ");
//        Clokc clokc = Clock.

        Instant instant = Instant.ofEpochMilli(System.currentTimeMillis());

        System.out.println(LocalDateTime.ofInstant(instant, ZoneId.systemDefault()));

        System.out.println(LocalDate.ofInstant(instant, ZoneId.systemDefault()));

    }

    /**
     * Period的between的计算天数计算的绝对差值，不受年月影响，所以是不准确的
     * 所以计算年、月是正确的
     */
    public static void calPeriod() {

        LocalDate start = LocalDate.of(2021, Month.JANUARY, 1);
        LocalDate end = LocalDate.of(2021, Month.FEBRUARY, 11);

        System.out.println(Period.between(start, end).getMonths());
        System.out.println(Period.between(start, end).getDays());
    }

    public static void calDay() {
        LocalDate start1 = LocalDate.of(2021, Month.JANUARY, 1);
        LocalDate end1 = LocalDate.of(2021, Month.JANUARY, 11);
        LocalDate start2 = LocalDate.of(2021, Month.JANUARY, 1);
        LocalDate end2 = LocalDate.of(2021, Month.FEBRUARY, 1);
        LocalDate start3 = LocalDate.of(2021, Month.JANUARY, 1);
        LocalDate end3 = LocalDate.of(2021, Month.JANUARY, 1);


        System.out.println("相隔天数：" + (end1.toEpochDay() - start1.toEpochDay()));
        System.out.println("相隔天数：" + (end2.toEpochDay() - start2.toEpochDay()));
        System.out.println("相隔天数：" + (end3.toEpochDay() - start3.toEpochDay()));
    }


    @Test
    public void plusDay() {
        LocalDate localDate =
            LocalDate.ofInstant(Instant.ofEpochMilli(Clock.systemDefaultZone().millis()), ZoneId.systemDefault());
        System.out.println(localDate);
        System.out.println(localDate.plusDays(1));

        LocalDate date = LocalDate.of(2021, 1, 3);
        System.out.println(date);
        String[] split = date.toString().split("-");
        Arrays.stream(split).forEach(System.out::println);

        // 解析成数字
        Arrays.stream(split).forEach(s -> {
            System.out.println(Integer.parseInt(s));
        });
    }


}
